﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Soussous.Business;
using Soussous.Tools;
using Soussous.DAO;
using Soussous.DAO.Impl;
using System.Collections.Generic;
namespace Soussous.Template
{
    public partial class friend : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {   
            User theUser = (User)HttpContext.Current.Session["user"];    
            
            if (Request.QueryString["id"] != null) 
            {
                FriendDAO.getInstance().deleteFriend(theUser.Id, int.Parse(Request.QueryString["id"])); 
            
            }

            
            
            
            
            
            List<Friend> listFriend = FriendDAO.getInstance().getMyFriend(theUser.Id);
            foreach (Friend fr in listFriend) 
            {
                
                HtmlTableRow myTableRow = new HtmlTableRow();
                HtmlTableCell cellule1 = new HtmlTableCell();
                HtmlTableCell cellule2 = new HtmlTableCell();
                HtmlTableCell cellule3 = new HtmlTableCell();
                HtmlTableCell cellule4 = new HtmlTableCell();

                cellule1.InnerHtml = fr.un_user_to.Lastname.ToString();
                cellule2.InnerHtml = fr.un_user_to.Firstname.ToString();
                cellule3.InnerHtml = fr.un_user_to.Email.ToString();
                cellule4.InnerHtml = "<a href=\"friend.aspx?id=" + fr.User_to + "\"><img src=\"./img/delete.gif\" /></a>";
                myTableRow.Cells.Add(cellule1);
                myTableRow.Cells.Add(cellule2);
                myTableRow.Cells.Add(cellule3);
                myTableRow.Cells.Add(cellule4);
                listing.Rows.Add(myTableRow);

            }

        }
        protected void Submit_form(object sender, EventArgs e)
        {
            User theUser = (User)HttpContext.Current.Session["user"];                
            string msg = "";
          


            if (emails.Text == "")
            {
                msg += "<li>Please fill the email address</li>";
            }
            else
            {
                if (!Helper.checkMail(emails.Text))
                {
                    msg += "<li>The email address filled seems invalid  </li>";

                }
                else 
                {
                    if (emails.Text != theUser.Email)
                    {
                        User user = UserDAO.getInstance().searchMailUser1(emails.Text);
                        if (user == null)
                        {
                            msg += "<li>The email address can't in our database, we send a email to your friend</li>";
                            string exp = "slowtun@gmail.com";
                            string to = emails.Text;
                            string sub = "Invitation to subscribe to Soussous";
                            string body = "Dear friend,\n\n";
                            body += "Your friend " + theUser.Lastname + " invite you to subcribe to Soussous .\n";
                            body += "We hop that you will spend of good time on our site.\n\n";
                            body += "Soussous team";
                            Helper.SendMail(exp, to, sub, body);
                        }
                        else
                        {
                            Friend myFriend = FriendDAO.getInstance().isMyFriend(theUser.Id, user.Id);

                            if (myFriend == null)
                            {
                                Friend fri = new Friend();
                                fri.User_from = theUser.Id;
                                fri.User_to = user.Id;
                                FriendDAO.getInstance().insert(fri);
                                Response.Redirect("friend.aspx");
                            }
                            else
                            {
                                msg += "<li>This personn is already your friend </li>";

                            }

                        }
                    }
                    else
                    {
                        msg += "<li>You can't add yourself </li>";
                    }
                }
            }
            error_msg.InnerHtml = msg;
        }
    }
}
